require 'action_controller'
# This plugin helps you leverage a setting of action controller. asset_host.
# Normally in rails you have one location, and one location only for all of your assets, 
# being images, stylesheet or javascripts.
# Since browsers usually have a limit of 2 downloading threads per domain, it means that 
# the more domains you serve your assets, the more concurrent downloads you can have
# the quicker the global page download happens.
# Couple this technique with minizing and gzipping the files for maximum result.

module MultiAssetLocations
  module Helpers
    # Provides methods for linking a HTML page together with other assets, such as javascripts, stylesheets, and feeds.
    module AssetTagHelper

    private
      def compute_public_path(source, dir, ext)
        source = source.to_s if source.is_a?(Symbol)
        source  = "/#{dir}/#{source}" unless source.first == "/" || source.include?(":")
        source << ".#{ext}" unless source.split("/").last.include?(".")
        source << '?' + rails_asset_id(source) if defined?(RAILS_ROOT) && %r{^[-a-z]+://} !~ source
        source  = "#{@controller.request.relative_url_root}#{source}" unless %r{^[-a-z]+://} =~ source
        unless source.include?(":")
          asset_location = ((ActionController::Base.asset_host.is_a?(String) && ActionController::Base.asset_host) ||
                            (ActionController::Base.asset_host.is_a?(Hash) && ActionController::Base.asset_host[dir.to_sym])).to_s 
          source = asset_location + source 
        end
        source
      end

    end
  end
end